//
//  LRNewFeatureViewController.swift
//  LRWeibo
//
//  Created by admin on 15/9/24.
//  Copyright © 2015年 懒人. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let WBNewFeatureImageCount = 4

class LRNewFeatureViewController: UICollectionViewController {

    init()
    {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return WBNewFeatureImageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        // Configure the cell
        cell.imageIndex = indexPath.item
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
       
        // 获取当前显示的index
        let path  = collectionView.indexPathsForVisibleItems()[0]
        if path.item == WBNewFeatureImageCount - 1
        {
            // TODO: - 这里为神马不行，有神马区别。 这个cell不是当前的cell？是上一个的？
//            (cell as! NewFeatureCell).starAnim()
            (collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell).starAnim()
        }
    }


}

/// 自定义cell
class NewFeatureCell: UICollectionViewCell{
    
    private var imageIndex = 0 {
        didSet{
            imageView.image = UIImage(named: "new_feature_\(imageIndex + 1)")

//            starButton.hidden = imageIndex == WBNewFeatureImageCount - 1 ? false : true
            starButton.hidden = true
        }
  }
    
    // MAR - 启动按钮动画
    private func starAnim()
    {

        starButton.hidden = false
        starButton.transform = CGAffineTransformMakeScale(0, 0)
        starButton.userInteractionEnabled = false
        

        UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            self.starButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                self.starButton.userInteractionEnabled = true
        }

    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(imageView)
        contentView.addSubview(starButton)
        imageView.frame = self.bounds
        starButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).offset(-160)

        }
        
        starButton.addTarget(self, action: "clickStartButton", forControlEvents: .TouchUpInside)
    }

    // 点击按钮事件
    @objc private func clickStartButton()
    {
        if UserAccountViewModel.sharedUserAccount.isLogin
        {
            NSNotificationCenter.defaultCenter().postNotificationName(LRRootViewControllChange, object: nil)
        }
        else
        {
            NSNotificationCenter.defaultCenter().postNotificationName(LRRootViewControllChange, object: self)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
     // MARK: - 控件懒加载
    private lazy var imageView = UIImageView()
    private lazy var starButton: UIButton = UIButton(title: "开始体验", fontSize: 15, color: UIColor.whiteColor(), backImageName: "new_feature_finish_button")
}
